﻿#pragma once
#include <iostream>
#include <assert.h>
using namespace std;

template<class K, class V>
struct AVLTree_Node
{
	typedef AVLTree_Node<K, V> Node;
	pair<K, V> _kv;
	Node* _left;
	Node* _right;
	Node* _parent;
	int _bf;

	AVLTree_Node(const pair<K, V> kv)
		:_kv(kv),
		_left(nullptr),
		_right(nullptr),
		_parent(nullptr),
		_bf(0)
	{}
};

template<class K, class V>
class AVLTree
{
	typedef AVLTree_Node<K, V> Node;

public:
	bool insert(const pair<K, V>& kv)
	{
		if (_root == nullptr)
		{
			_root = new Node(kv);
			return true;
		}
		Node* cur = _root;
		Node* parent = cur;

		while (cur)
		{
			if (kv.first > cur->_kv.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (kv.first < cur->_kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				return false;
			}
		}

		cur = new Node(kv);
		if (kv.first < parent->_kv.first)
		{
			parent->_left = cur;
			cur->_parent = parent;
			//parent->_bf--;
		}
		else if (kv.first > parent->_kv.first)
		{
			parent->_right = cur;
			cur->_parent = parent;
			//parent->_bf++;
		}

		parent = cur->_parent;
		while (parent)
		{
			if (parent->_left == cur)
				parent->_bf--;
			else if (parent->_right == cur)
				parent->_bf++;

			if (parent->_bf == 0)
				break;
			else if (parent->_bf == 1 || parent->_bf == -1)
			{
				cur = parent;
				parent = parent->_parent;
			}
			else if (parent->_bf == 2 || parent->_bf == -2)
			{
				if (parent->_bf == -2 && cur->_bf == -1)
				{
					RotateR(parent);
					return true;
					//parent->_bf = cur->_bf = 0;
				}
				else if (parent->_bf == 2 && cur->_bf == 1)
				{
					RotateL(parent);
					return true;
					//parent->_bf = cur->_bf = 0;
				}
				else if (parent->_bf == -2 && cur->_bf == 1)
				{
					RotateLR(parent);
					return true;

				}
				else if (parent->_bf == 2 && cur->_bf == -1)
				{
					RotateRL(parent);
					return true;
				}
			}
			else
			{
				assert(false);
			}

		}
		return true;
	}
	
	bool find(const K& k)
	{
		Node* cur = _root;

		while (cur)
		{
			if (k < cur->_kv.first)
			{
				cur = cur->_left;
			}
			else if (k > cur->_kv.first)
			{
				cur = cur->_right;
			}
			else
			{
				return true;
			}
		}
		
		return false;
	}
	
	void Inorder()
	{
		_inorder(_root);
	}
	int Height()
	{
		return _Height(_root);
	}
	bool IsBalanceTree()
	{
		return _IsBalanceTree(_root);
	}
private:
	
	bool _IsBalanceTree(Node* root)
	{
		// 空树也是AVL树
		if (nullptr == root)
			return true;
		// 计算pRoot结点的平衡因⼦：即pRoot左右⼦树的⾼度差
		int leftHeight = _Height(root->_left);
		int rightHeight = _Height(root->_right);
		int diff = rightHeight - leftHeight;
		// 如果计算出的平衡因⼦与pRoot的平衡因⼦不相等，或者
		// // pRoot平衡因⼦的绝对值超过1，则⼀定不是AVL树
		if (abs(diff) >= 2)
		{
			cout << root->_kv.first << "高度差异常" << endl;
			return false;
		} 
		if(root->_bf != diff)
		{
			cout << root->_kv.first << "平衡因子异常" << endl;
			return false;
		} 
		// pRoot的左和右如果都是AVL树，则该树⼀定是AVL树
		return _IsBalanceTree(root->_left) && _IsBalanceTree(root->_right);
	}
	void _inorder(Node* root)
	{
		if (root == nullptr)
			return;
		_inorder(root->_left);
		cout << root->_kv.first<<" ";
		_inorder(root->_right);
	}
	

	int _Height(Node* root)
	{
		if (root == nullptr)
			return 0;

		int left_height = _Height(root->_left);
		int right_height = _Height(root->_right);

		return left_height > right_height ? left_height + 1 : right_height + 1;
	}

	void RotateR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;

		parent->_left = subLR;
		if(subLR)
			subLR->_parent = parent;

		Node* pp = parent->_parent;
		subL->_right = parent;
		parent->_parent = subL;
		if (pp == nullptr)
		{
			_root = subL;
			subL->_parent = nullptr;
		}
		else
		{
			if (pp->_left == parent)
			{
				pp->_left = subL;
			}
			else if (pp->_right == parent)
			{
				pp->_right = subL;
			}

			subL->_parent = pp;
		}
		parent->_bf = subL->_bf = 0;
	}
	void RotateL(Node* parent)
	{

		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		parent->_right = subRL;
		if(subRL)
			subRL->_parent = parent;

		Node* pp = parent->_parent;
		subR->_left = parent;
		parent->_parent = subR;
		if (pp == nullptr)
		{
			_root = subR;
			subR->_parent = nullptr;
		}
		else
		{
			if (pp->_left == parent)
			{
				pp->_left = subR;
			}
			else if (pp->_right == parent)
			{
				pp->_right = subR;
			}
			subR->_parent = pp;
		}

		parent->_bf = subR->_bf = 0;
	}

	void RotateLR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;
		int bf = subLR->_bf;

		RotateL(subL);
		RotateR(parent);

		if (bf == 0)
		{
			parent->_bf = 0;
			subL->_bf = 0;
			subLR->_bf = 0;
		}
		else if (bf == -1)
		{
			parent->_bf = 1;
			subL->_bf = 0;
			subLR->_bf = 0;
		}
		else if (bf == 1)
		{
			subL->_bf = -1;
			parent->_bf = 0;
			subLR->_bf = 0;
		}
		else
		{
			assert(false);
		}
	}
	void RotateRL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;
		int bf = subRL->_bf;

		RotateR(subR);
		RotateL(parent);

		if (bf == 0)
		{
			parent->_bf = 0;
			subR->_bf = 0;
			subRL->_bf = 0;
		}
		else if (bf == 1)
		{
			parent->_bf = -1;
			subR->_bf = 0;
			subRL->_bf = 0;
		}
		else if (bf == -1)
		{
			parent->_bf = 0;
			subR->_bf = 1;
			subRL->_bf = 0;
		}
		else
		{
			assert(false);
		}
	}
private:
	Node* _root = nullptr;
};

/*bool Inorder_check()
{
	_inorder_check(_root);
}*/
/*bool _inorder_check(Node* root)
	{
		if (root == nullptr)
			return true;
		if (!_inorder_check(root->_left))
		{
			return false;
		}
		if (abs(root->_bf) >= 2)
			return false;
		else
			return true;
		if (!_inorder_check(root->_right))
		{
			return false;
		}
	}*/